package com.zhentao.controller;

import com.zhentao.dto.DoctorInfoDTO;
import com.zhentao.dto.DoctorLoginDTO;
import com.zhentao.dto.DoctorPasswordChangeDTO;
import com.zhentao.dto.DoctorRegisterDTO;
import com.zhentao.pojo.Doctor;
import com.zhentao.result.Result;
import com.zhentao.service.DoctorService;
import com.zhentao.vo.DoctorLoginVO;
import com.zhentao.vo.DoctorProfileVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 医生控制器
 */
@Slf4j
@RestController
@RequestMapping("/doctor")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    /**
     * 医生注册接口
     * 
     * @param registerDTO 注册信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result register(@Validated @RequestBody DoctorRegisterDTO registerDTO) {
        try {
            log.info("医生注册请求：{}", registerDTO.getDoctorName());
            
            Doctor doctor = doctorService.register(registerDTO);
            
            // 不返回敏感信息（密码、盐值等）
            doctor.setPassword(null);
            doctor.setSalt(null);
            
            return Result.success("注册成功", doctor);
            
        } catch (Exception e) {
            log.error("医生注册失败：{}", registerDTO.getDoctorName(), e);
            return Result.failed(e.getMessage());
        }
    }

    /**
     * 医生登录接口
     * 
     * @param loginDTO 登录信息
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result login(@Validated @RequestBody DoctorLoginDTO loginDTO) {
        try {
            log.info("医生登录请求：{}", loginDTO.getDoctorName());
            
            DoctorLoginVO loginVO = doctorService.login(loginDTO);
            
            return Result.success("登录成功", loginVO);
            
        } catch (Exception e) {
            log.error("医生登录失败：{}", loginDTO.getDoctorName(), e);
            return Result.failed(e.getMessage());
        }
    }

    /**
     * 获取医生个人资料接口
     * 
     * @param doctorId 医生ID
     * @return 个人资料
     */
    @GetMapping("/profile/{doctorId}")
    public Result getProfile(@PathVariable Integer doctorId) {
        try {
            log.info("获取医生个人资料请求：doctorId={}", doctorId);
            
            DoctorProfileVO profileVO = doctorService.getProfile(doctorId);
            
            return Result.success("获取成功", profileVO);
            
        } catch (Exception e) {
            log.error("获取医生个人资料失败：doctorId={}", doctorId, e);
            return Result.failed(e.getMessage());
        }
    }

    /**
     * 修改医生个人信息接口
     * 
     * @param doctorId 医生ID
     * @param infoDTO 个人信息
     * @return 修改结果
     */
    @PutMapping("/profile/{doctorId}")
    public Result updateProfile(@PathVariable Integer doctorId, 
                              @Validated @RequestBody DoctorInfoDTO infoDTO) {
        try {
            log.info("修改医生个人信息请求：doctorId={}", doctorId);
            
            boolean success = doctorService.updateProfile(doctorId, infoDTO);
            
            if (success) {
                return Result.success("修改成功");
            } else {
                return Result.failed("修改失败");
            }
            
        } catch (Exception e) {
            log.error("修改医生个人信息失败：doctorId={}", doctorId, e);
            return Result.failed(e.getMessage());
        }
    }

    /**
     * 修改医生密码接口
     * 
     * @param doctorId 医生ID
     * @param passwordChangeDTO 密码修改信息
     * @return 修改结果
     */
    @PutMapping("/password/{doctorId}")
    public Result changePassword(@PathVariable Integer doctorId,
                               @Validated @RequestBody DoctorPasswordChangeDTO passwordChangeDTO) {
        try {
            log.info("修改医生密码请求：doctorId={}", doctorId);
            
            boolean success = doctorService.changePassword(doctorId, passwordChangeDTO);
            
            if (success) {
                return Result.success("密码修改成功");
            } else {
                return Result.failed("密码修改失败");
            }
            
        } catch (Exception e) {
            log.error("修改医生密码失败：doctorId={}", doctorId, e);
            return Result.failed(e.getMessage());
        }
    }

    /**
     * 上传医生头像接口
     * 
     * @param doctorId 医生ID
     * @param file 头像文件
     * @return 上传结果
     */
    @PostMapping("/avatar/{doctorId}")
    public Result uploadAvatar(@PathVariable Integer doctorId,
                             @RequestParam("file") MultipartFile file) {
        try {
            log.info("上传医生头像请求：doctorId={}, fileName={}", doctorId, file.getOriginalFilename());
            
            // 验证文件是否为空
            if (file.isEmpty()) {
                return Result.failed("请选择要上传的文件");
            }
            
            String avatarUrl = doctorService.uploadAvatar(doctorId, file);
            
            return Result.success("头像上传成功", avatarUrl);
            
        } catch (Exception e) {
            log.error("上传医生头像失败：doctorId={}", doctorId, e);
            return Result.failed(e.getMessage());
        }
    }
}
